home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / brute.com / BRUTE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-07  |  2.3 KB  |  94 lines

  1. /**         THE BRUTE FORCE FUNCTION !
  2.  **
  3.  **    For searching for text strings within binary files, or if you just
  4.  **  don't LIKE the StrStr(); function.  The logic for this was taken from
  5.  **  an Algorithm book I found within University of Texas' Technical
  6.  **  Library.
  7.  **
  8.  **       So when you gotta have that search... now you got it !
  9.  **
  10.  **            (courtesy of Dave Smith, CIS 71441,2723)
  11.  **/
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. int brute(char *a, char *p);  /* Our function */
  17.  
  18. void main(void)
  19. {
  20.  
  21. char *shortee="store";       /* What we search for */
  22.  
  23. /** Our test strings to search
  24.  **          into
  25.  **/
  26.  
  27. char *longee="My father went to the store";
  28. char *longe2="The stora caught on fire !";
  29. char *longe3="Where's the #@!^^5*store&$@`~% ?";
  30.  
  31.   /* Our first test involves longee */
  32.  
  33. if(brute(longee, shortee)) printf("\nFound %s in %s",shortee, longee);
  34. else printf("\nNo find");
  35.  
  36.   /* Longe2 has "STORA", which is similar, but not similar enough to "STORE" */
  37.  
  38. if(brute(longe2, shortee)) printf("\nFound %s in %s",shortee, longe2);
  39. else printf("\nNo find 2");
  40.  
  41.   /**  And finally, longe3 has "STORE" within it, closely surrounded
  42.    **                   by other characters.
  43.    **/
  44.  
  45. if(brute(longe3, shortee)) printf("\nFound %s in %s",shortee, longe3);
  46. else printf("\nNo find 3");
  47.  
  48. }
  49.  
  50.  
  51.  
  52. /*=================== Brute Force Function ==============================*/
  53. /*=======================================================================*/
  54. /*=========== Call with (LONGER STRING, SHORTER STRING) =================*/
  55.  
  56. int brute(char *a, char *p)
  57. {
  58.  
  59. int m, n, i=1, j=1;    /* Set our positions to 1. */
  60.  
  61. n=strlen(a)-1;         /* N is the string length of longer string */
  62. m=strlen(p)-1;         /* M is the string length of shorter string */
  63.  
  64. /**  If first chars match, increment in order to
  65.  **  see if second, third, fourth... chars match.
  66.  **/
  67.  
  68. for(;;){
  69.   if(a[i]==p[j]){
  70.     i++;
  71.     j++;
  72.                 }
  73.  
  74. /**  No, they don't match: restart shorter pointer,
  75.  **  and point longer to next character.
  76.  **/
  77.  
  78.  else{
  79.  i=i-j+2;
  80.  j=1;
  81.      }
  82.  
  83. if(j>m) return 1;      /* We found a match, return with 1 */
  84.  
  85.  
  86. /**  No match found and end-of-longer has been reached.
  87.  **             Go home with 0.
  88.  **/
  89.  
  90. if(i>n) return 0;
  91.  
  92.  }
  93. }
  94.